package org.yoki.tool.core.helper;

import org.yoki.tool.core.bean.TableTemplateBean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.*;

/**
 * Created by SKY on 2017/6/26.
 */
public class TableBuildHelper {

    private Connection connection;

    public TableBuildHelper(Connection connection) {
        this.connection = connection;
    }

    public List<TableTemplateBean> buildTableList(){
        List<TableTemplateBean> list = new ArrayList<>() ;
        try {
            String currentCatalog = connection.getCatalog() ;
            ResultSet resultSet = connection.getMetaData().getTables(currentCatalog , null , null , new String[]{ "TABLE" }) ;
            ColumnBuildHelper columnBuildHelper = new ColumnBuildHelper(connection) ;
            while (resultSet.next()) {
                TableTemplateBean table = new TableTemplateBean() ;
                table.setTableCat(resultSet.getString(1));
                table.setTableSchema(resultSet.getString(2));
                table.setTableName(resultSet.getString(3));
                table.setTableType(resultSet.getString(4));
                table.setRemarks(resultSet.getString(5));

                table.setTypeCatalog(resultSet.getString(6));
                table.setTypeSchema(resultSet.getString(7));
                table.setTypeName(resultSet.getString(8));
                table.setSelfReferencingColName(resultSet.getString(9));
                table.setRefGeneration(resultSet.getString(10));

                table.setColumnTemplateBeanList(columnBuildHelper.buildColumnList(table.getTableName())) ;

                list.add(table) ;
            }
            resultSet.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list ;

    }


}
